/**
 * 
 */
package com.jbarreto.client;

import java.util.Properties;

import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * @author 814168
 *
 */
public class LogClient {

	/**
	 * @param args
	 * @throws NamingException 
	 */
	public static void main(String[] args) throws Exception {
	
		Properties jndiProps = new Properties();
		jndiProps.setProperty(Context.INITIAL_CONTEXT_FACTORY,
		"org.jnp.interfaces.NamingContextFactory");
		jndiProps.setProperty(Context.URL_PKG_PREFIXES,
		"org.jboss.naming:org.jnp.interface");
		jndiProps.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
		
		InitialContext ctx = new InitialContext(jndiProps);
	
        // 1: Lookup connection factory
        QueueConnectionFactory factory =
            (QueueConnectionFactory) ctx.lookup
                 ("/ConnectionFactory");

        // 2: Use connection factory to create JMS connection
        QueueConnection connection = factory.createQueueConnection();

        // 3: Use connection to create a session
        QueueSession session =
            connection.createQueueSession
                 (false,Session.AUTO_ACKNOWLEDGE);

        // 4: Lookup destination
        Queue queue = (Queue)ctx.lookup("queue/JBQueue");

        // 5: Create a message publisher
        QueueSender  producer = session.createSender(queue);

        // 6: Create and publish a message
        TextMessage msg = session.createTextMessage();
        msg.setText("This is a test message for a Queue MDB.");
        producer.send(msg);

        // finish
        producer.close();
        System.out.println("Message published. Please check application server's console to see the response from MDB.");
        
        //Close connetion
        connection.close();

	}

}
